package myky.com.doctorapp.ui

import android.graphics.Color
import android.support.v4.widget.NestedScrollView
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import com.github.nukc.stateview.Constant
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_doctor_team.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.DoctoreTeamMenberAdapter
import myky.com.doctorapp.adapter.TeamDoctorScaduleAdapter
import myky.com.doctorapp.api.Address
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.DoctoeTeamContract
import myky.com.doctorapp.entity.DoctorTeamEntity
import myky.com.doctorapp.presenter.DoctoeTeamPresenter
import myky.com.doctorapp.utils.Constants
import myky.com.doctorapp.utils.UriUtils
import java.util.*


/**
 * Description: 医生团队
 * @author xiangyao
 * Created by 2019/2/20 3:40 PM
 */
class DoctorTeamActivity : BaseActivity<DoctoeTeamPresenter, DoctorTeamEntity>(), DoctoeTeamContract.View, View.OnClickListener {
    private var level_lx: String = ""

    private var isOpen_members: Boolean = false

    lateinit var doctorTeamAdapter: DoctoreTeamMenberAdapter

    var doctorTeamAll = ArrayList<DoctorTeamEntity.OtherdoctorsBean>()

    var adpterData = ArrayList<DoctorTeamEntity.OtherdoctorsBean>()

    var somepartdoctor = ArrayList<DoctorTeamEntity.OtherdoctorsBean>()

    private lateinit var strings: MutableList<Array<String>>

    private var doctorVisits: MutableList<DoctorTeamEntity.DoctorvisitsBean> = ArrayList()

    lateinit var teamDoctorScaduleAdapter: TeamDoctorScaduleAdapter

    var institutionid = -1






    override fun onClick(p0: View?) {

        when(p0?.id){
            R.id.bespeak_team_members_open->{

                Log.i("xiangyao","查看更多")


                adpterData.clear()

                if (isOpen_members) {

                    adpterData.addAll(somepartdoctor)


                    bespeak_team_members_open_iv.setImageResource(R.drawable.yyghlb_xjt)
                    bespeak_team_members_open_tv.text = "展开,查看其他成员"

                    isOpen_members = false
                } else {


                    adpterData.addAll(doctorTeamAll)

                    bespeak_team_members_open_iv.setImageResource(R.drawable.yyghlb_xjt_xs)
                    bespeak_team_members_open_tv.text = "收起"

                    isOpen_members = true

                }


                doctorTeamAdapter.notifyDataSetChanged()
            }

        }
    }



    override fun showTeamMenber(otherdoctors: MutableList<DoctorTeamEntity.OtherdoctorsBean>?) {


        //团队成员
        if (otherdoctors != null) {
            if (otherdoctors.size > 2) {
                doctorTeamAll.addAll(otherdoctors)
                somepartdoctor.addAll(otherdoctors.subList(0, 2))
                adpterData.addAll(somepartdoctor)

            } else {

                somepartdoctor.addAll(otherdoctors)
                adpterData.addAll(somepartdoctor)
                bespeak_team_members_open.visibility = View.GONE
            }
        }


        doctorTeamAdapter.notifyDataSetChanged()

    }

    override fun showScadule(mDoctorvisits: MutableList<DoctorTeamEntity.DoctorvisitsBean>?) {

        if (mDoctorvisits != null) {
            for (i in mDoctorvisits.indices) {
                val visitType = mDoctorvisits[i].visittype
                val visitWeek = mDoctorvisits[i].visitweek
                val visitTime = mDoctorvisits[i].visittime
                if (visitType != 0) {
                    if (visitTime == 0) {
                        if (doctorVisits[visitWeek].visittime === 0) {
                            doctorVisits[visitWeek].visittime = 1
                        } else if (doctorVisits[visitWeek].visittime === 2) {
                            doctorVisits[visitWeek].visittime = 3
                        }
                    } else if (visitTime == 1) {
                        if (doctorVisits[visitWeek].visittime === 0) {
                            doctorVisits[visitWeek].visittime = 2
                        } else if (doctorVisits[visitWeek].visittime === 1) {
                            doctorVisits[visitWeek].visittime = 3
                        }
                    }
                }
            }
            teamDoctorScaduleAdapter.notifyDataSetChanged()
        }

    }


    override fun showTeamInfo(doctorTeamEntity: DoctorTeamEntity) {


        if (doctorTeamEntity.doctor != null) {
            bespeak_team_lxzj_levels.text = resources.getStringArray(R.array.academicName_level)[doctorTeamEntity.doctor.academicname]
            level_lx = resources.getStringArray(R.array.administrationName_level)[doctorTeamEntity.doctor.administrationname]

            bespeak_team_lxzj_level.text = level_lx

            bespeak_team_lxzj_name.text = doctorTeamEntity.doctor.realname

            bespeak_team_lxzj_hospital.text = doctorTeamEntity.doctor.hospitalname

            bespeak_team_lxzj_ks.text = doctorTeamEntity.doctor.departmentname

            bespeak_team_iv_lxzj.setImageURI(UriUtils.getUriInstance().getUri(Constant.ONLINEPIC, Address.IMAGE + doctorTeamEntity.doctor.headPhoto))
        }



        bespeak_team_ke_tv.text = doctorTeamEntity.allname

        bespeak_team_num.text = doctorTeamEntity.doctornum.toString()

        bespeak_team_goodAt.text = doctorTeamEntity.specialty

        bespeak_team_etv.text = doctorTeamEntity.specialty

        bespeak_team_hospital_ads.text = doctorTeamEntity.address

        activity_teams_logo.setImageURI(UriUtils.getUriInstance().getUri(Constant.ONLINEPIC, Address.IMAGE + doctorTeamEntity.pictureMD5))


    }



    override fun reloadActivity() {
    }

    override fun injectTarget(): View = findViewById(R.id.injectView)

    override fun initData() {

        val extras = intent.extras

        institutionid = extras.getInt(Constants.TEAMID)


        mPresenter.getTeamInfo("v1.0", institutionid.toString())
    }

    override fun initView() {

        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(R.id.my_self_title)
                .init()

        doctorVisits.clear()
        for (i in 0..7) {
            doctorVisits.add(DoctorTeamEntity.DoctorvisitsBean())
        }
        strings = Arrays.asList(resources.getStringArray(R.array.visit_time))


        bespeak_team_members.layoutManager = LinearLayoutManager(this)


        bespeak_team_members.isNestedScrollingEnabled=false

        doctorTeamAdapter = DoctoreTeamMenberAdapter(adpterData)


        bespeak_team_members.adapter = doctorTeamAdapter


        bespeak_team_members_open.setOnClickListener(this)


        bespeak_team_visit_time.layoutManager=GridLayoutManager(this,8)

        teamDoctorScaduleAdapter= TeamDoctorScaduleAdapter(doctorVisits,strings)


        bespeak_team_visit_time.adapter=teamDoctorScaduleAdapter





        nested_scrollview.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, _ ->
            val bottom = doctorBg.bottom
            var total = 0
            total += scrollY

            when {
                total <= 0 -> titleText.setTextColor(Color.argb(0, 255, 255, 255))
                bottom >= total -> {
                    val d = total.toDouble() / bottom
                    val alpha = d * 255
                    titleText.setTextColor(Color.argb(alpha.toInt(), 255, 255, 255))
                }
                else -> titleText.setTextColor(Color.argb(255, 255, 255, 255))
            }
        })

    }

    override fun getContenView(): Int = R.layout.activity_doctor_team

    override fun showDataSuccess(datas: DoctorTeamEntity?) {
    }


    override fun getPresenter(): DoctoeTeamPresenter {
        return DoctoeTeamPresenter()
    }
}
